/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BESA.SystemLevel.Directory.greenpages;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 *
 * @author Andrea
 */
public class Society implements Serializable{

	private static final long serialVersionUID = -5216238719049652023L;
	/**
	 * groups that are part of the society
	 */
	private Set<Group> groups;
	private Set<String> groupsNames;
	/**
	 * A collection of descriptions
	 */
	//private List<String> description;
	/**
	 * A name (unique id of the society)
	 */
	private String name;

	/**
	 * Create a society with the given name
	 * @param name
	 */
	public Society(String name) {
		super();
		this.name = name;
		//this.description = new ArrayList<String>();
		this.groups = new HashSet<Group>();
		groupsNames  = new HashSet<String>();
	}

	public boolean registerGroup(Group toAdd){
		return groups.add(toAdd) && groupsNames.add(toAdd.getGroupName());
	}
	
	
	
	public boolean unregisterGroup(String toRemove){
		boolean ret = false;
		for (Iterator<Group> iterator = groups.iterator(); iterator.hasNext() && !ret;) {
			Group type = iterator.next();
			if(type.getGroupName().equals(toRemove)){
				groups.remove(type);
				ret = true;
			}
		}
		return ret && groupsNames.remove(toRemove);
	}
	
	public boolean unregisterGroup(Group toRemove){
		return groups.remove(toRemove) && groupsNames.remove(toRemove.getGroupName());
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Society)
			return this.hashCode() == obj.hashCode();
		else
			return false;
	}
	 
	public List<String> getGroupsNames(){
		List<String>  ret = new ArrayList<String>();
		for (Iterator<String> iterator = groupsNames.iterator(); iterator.hasNext();) {
			 ret.add(iterator.next());
		}
		return ret;
		
	}
	

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return this.name.hashCode();
	}



}
